﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace window
{
    public class SJT_Window
    {
        /// <summary>
        /// 根据坐标获取窗口句柄
        /// </summary>
        /// <param name="Point"></param>
        /// <returns></returns>
        [DllImport("user32")]
        private static extern IntPtr WindowFromPoint(Point Point);

        /// <summary>
        /// FindWindowEx(maindHwnd, IntPtr.Zero, null, "登录")
        /// </summary>
        /// <param name="hwndParent"></param>
        /// <param name="hwndChildAfter"></param>
        /// <param name="lpszClass"></param>
        /// <param name="lpszWindow"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("coredll.dll", EntryPoint = "FindWindow")]
        public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
        /// <summary>
        /// 0 、关闭窗口 1、正常大小显示窗口 2、最小化窗口 3、最大化窗口
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="nCmdShow"></param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="Msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        /// <summary>
        /// 获取当前窗口句柄:GetForegroundWindow()
        /// </summary>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern IntPtr GetForegroundWindow();
        /// <summary>
        /// 获取指定窗体的标题
        /// </summary>
        /// <param name="WinHandle">窗体句柄</param>
        /// <param name="Title">缓冲区取用于存储标题</param>
        /// <param name="size">缓冲区大小</param>
        /// <returns></returns>
        [DllImport("User32.dll")]
        private static extern int GetWindowText(IntPtr WinHandle, StringBuilder Title, int size);
        /// <summary>
        /// 根据句柄获得进程id值
        /// </summary>
        /// <param name="handle">句柄</param>
        /// <param name="pid"></param>
        /// <returns></returns>
        [DllImport("user32")]
        private static extern int GetWindowThreadProcessId(IntPtr handle, out int pid);
        [DllImport("user32.dll")]
        private static extern void GetClassName(IntPtr hwnd, StringBuilder s, int nMaxCount);   //获取句柄类名


        public const int WM_KEYDOWN = 0X100;
        public const int WM_KEYUP = 0X101;
        public const int WM_SYSCHAR = 0X106;
        public const int WM_SYSKEYUP = 0X105;
        public const int WM_SYSKEYDOWN = 0X104;
        public const int WM_CHAR = 0X102;

        /// <summary>
        /// 当前窗体句柄
        /// </summary>
        public IntPtr WindowIntPtr { get; set; }
        /// <summary>
        /// 进程id
        /// </summary>
        public string ProcessId { get; set; }
        /// <summary>
        /// 进程名称
        /// </summary>
        public string ProcessName { get; set; }
        public SJT_Window()
        {
            Init();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            this.WindowIntPtr = SJT_Window.GetForegroundWindow();
            Process[] arrayProcess = Process.GetProcesses();
            int processId = 0;
            SJT_Window.GetWindowThreadProcessId(this.WindowIntPtr, out processId);
            var pubgProcess = arrayProcess.First(t => t.Id == processId);
            this.ProcessName = pubgProcess?.ProcessName;
        }
        /// <summary>
        /// 重新加载
        /// </summary>
        public void reLoad()
        {
            Init();
        }
        /// <summary>
        /// 发送消息，同时回车结束
        /// </summary>
        /// <param name="msg"></param>
        public void SendMessage(string msg)
        {
            InputStr(this.WindowIntPtr, msg); //
            SendEnter();
        }

        /// <summary>
        /// 发送回车
        /// </summary>
        public void SendEnter()
        {
            SJT_Window.SendMessage(this.WindowIntPtr, SJT_Window.WM_KEYDOWN, 0x0d, 0); //输入ENTER（0x0d）
            SJT_Window.SendMessage(this.WindowIntPtr, SJT_Window.WM_KEYUP, 0x0d, 0);
        }

        /// <summary>
        /// 发送Tab
        /// </summary>
        public void SendTab()
        {
            SJT_Window.SendMessage(this.WindowIntPtr, SJT_Window.WM_KEYDOWN, 0x09, 0); //发送Tab（0x09）
            SJT_Window.SendMessage(this.WindowIntPtr, SJT_Window.WM_KEYUP, 0x09, 0);
        }
        /// <summary>
        　/// 发送一个字符串
        　/// </summary>
        　/// <param name="intPtr">窗口句柄</param>
        　/// <param name="Input">字符串</param>
        private void InputStr(IntPtr intPtr, string Input)
        {
            byte[] ch = (ASCIIEncoding.ASCII.GetBytes(Input));
            for (int i = 0; i < ch.Length; i++)
            {
                SJT_Window.SendMessage(intPtr, SJT_Window.WM_CHAR, ch[i], 0);
            }
        }
    }
}
